package search;

import java.io.IOException;
import java.util.logging.Logger;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@SuppressWarnings("serial")
public class SearchServlet extends HttpServlet {

	private static final Logger log = Logger.getLogger(SearchServlet.class
			.getName());

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws IOException {
		try {
			String action = request.getParameter("action");
			String index = request.getParameter("index");
			if (index != null && !index.isEmpty()) {
				if (action != null && action.equals("remove_index")) {
					if (index.equals("order")) {
						OrderSearchEngine.removeAllDocument();
					} else if (index.equals("deal")) {
						DealSearchEngine.removeAllDocument();
					}
					response.getWriter()
							.println(
									"Remove all order document from search engine success");
				} else if (action != null && action.equals("reindex")) {
					if (index.equals("order")) {
						OrderSearchEngine.reindex();
					} else if (index.equals("deal")) {
						DealSearchEngine.reindex();
					}
					response.getWriter()
							.println(
									"Re-index all order document from search engine success");
				}
			}
		} catch (Exception e) {
			System.out.println("Error when use search servlet !");
			log.warning("Error when use search servlet !");
			log.warning(e.toString());
			e.printStackTrace();
		}
	}
}
